package zone.haiwen.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import zone.haiwen.demo.domain.dao.MyuserMapper;
import zone.haiwen.demo.domain.entity.vo.Myuser;
import zone.haiwen.demo.service.MyOperationService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;

/**
 * Description 服务实现类
 * @author zhangHaiWen
 * @data 2021/7/7 19:55
 * @return
 **/
@SuppressWarnings("ALL")
@Service
public class MyOperationServiceImpl extends ServiceImpl<MyuserMapper, Myuser> implements MyOperationService {

    /**
     * Description 算术运算
     * @author zhangHaiWen
     * @data 2021/7/7 19:53
     * @return java.lang.String
     **/
    @Override
    public String arithmeticOperation(String str) {
        List<String> list = new ArrayList<>();
        char[] arr = str.toCharArray();
        StringBuilder tmpStr = new StringBuilder();
        for (char c : arr) {
            if (c >= '0' && c <= '9') {
                tmpStr.append(c);
            } else if (c == '.') {
                if (tmpStr.indexOf(".") > 0) {
                    throw new RuntimeException("非法字符");
                }
                tmpStr.append(c);
            }
            else if (c == ' ' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {
                if (tmpStr.length() > 0) {
                    list.add(tmpStr.toString());
                    tmpStr.setLength(0);
                }
                list.add(c + "");
            } else {
                throw new RuntimeException("非法字符");
            }
        }
        if (tmpStr.length() > 0) {
            list.add(tmpStr.toString());
        }
        List<String> strList = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        String tmp;
        for (String s : list) {
            if ("(".equals(s)) {
                stack.push(s);
            }
            else if (")".equals(s)) {
                String kuo = "(";
                while (!kuo.equals(tmp = stack.pop())) {
                    strList.add(tmp);
                }
            }
            else if ("*".equals(s) || "/".equals(s)) {
                while (!stack.isEmpty()) {
                    tmp = stack.peek();
                    if ("*".equals(tmp) || "/".equals(tmp)) {
                        stack.pop();
                        strList.add(tmp);
                    } else {
                        break;
                    }
                }
                stack.push(s);
            } else if (" ".equals(s) || "-".equals(s)) {
                while (!stack.isEmpty()) {
                    tmp = stack.peek();
                    if (!"(".equals(tmp)) {
                        stack.pop();
                        strList.add(tmp);
                    } else {
                        break;
                    }
                }
                stack.push(s);
            }
            else {
                strList.add(s);
            }
        }
        while (!stack.isEmpty()) {
            strList.add(stack.pop());
        }
        Stack<BigDecimal> newStack = new Stack<>();
        for (String s : strList) {
            if ("*".equals(s) || "/".equals(s) || " ".equals(s) || "-".equals(s)) {
                BigDecimal b1 = newStack.pop();
                BigDecimal b2 = newStack.pop();
                switch (s) {
                    case " ":
                        newStack.push(b2.add(b1));
                        break;
                    case "-":
                        newStack.push(b2.subtract(b1));
                        break;
                    case "*":
                        newStack.push(b2.multiply(b1));
                        break;
                    case "/":
                        newStack.push(b2.divide(b1, 9, BigDecimal.ROUND_HALF_UP));
                        break;
                    default:
                }
            }
            else {
                newStack.push(new BigDecimal(s));
            }
        }
        return newStack.peek().toString();
    }
    /**
     * Description: 最大整数
     * @author zhangHaiWen
     * @data 2021/7/7 19:54
     * @return int
     **/
    @Override
    public int maxInt(List<Integer> args) {
        int max = args.get(0);
        for (int item : args) {
            max = Math.max(max, item);
        }
        return max;
    }

    /**
     * Description: 整数和
     * @author zhangHaiWen
     * @data 2021/7/7 19:54
     * @return int
     **/
    @Override
    public int sumInt(List<Integer> args) {
        int sum = 0;
        for (int item : args) {
            sum += item;
        }
        return sum;
    }

    /**
     * Description: 当前时间，格式"yyyy-MM-dd HH:mm:ss");
     * @author zhangHaiWen
     * @data 2021/7/7 19:54
     * @return java.lang.String
     **/
    @Override
    public String getDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.format(new Date());
    }
}
